package com.ruoyi.production.service.impl.blade;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.production.domain.blade.BladeAmount;
import com.ruoyi.production.mapper.blade.BladeAmountMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.production.mapper.blade.BladePurchaseMapper;
import com.ruoyi.production.domain.blade.BladePurchase;
import com.ruoyi.production.service.blade.IBladePurchaseService;

/**
 * 刀片购买信息Service业务层处理
 *
 * @author ruoyi
 * @date 2021-07-12
 */
@Service
public class BladePurchaseServiceImpl implements IBladePurchaseService
{
    @Autowired
    private BladePurchaseMapper bladePurchaseMapper;
    @Autowired
    private BladeAmountMapper bladeAmountMapper;

    /**
     * 查询刀片购买信息
     *
     * @param bladePurchaseId 刀片购买信息ID
     * @return 刀片购买信息
     */
    @Override
    public BladePurchase selectBladePurchaseById(Long bladePurchaseId)
    {
        return bladePurchaseMapper.selectBladePurchaseById(bladePurchaseId);
    }

    /**
     * 查询刀片购买信息列表
     *
     * @param bladePurchase 刀片购买信息
     * @return 刀片购买信息
     */
    @Override
    public List<BladePurchase> selectBladePurchaseList(BladePurchase bladePurchase)
    {
        return bladePurchaseMapper.selectBladePurchaseList(bladePurchase);
    }

    /**
     * 新增刀片购买信息
     *
     * @param bladePurchase 刀片购买信息
     * @return 结果
     */
    @Override
    public int insertBladePurchase(BladePurchase bladePurchase)
    {
        return bladePurchaseMapper.insertBladePurchase(bladePurchase);
    }

    /**
     * 修改刀片购买信息
     *
     * @param bladePurchase 刀片购买信息
     * @return 结果
     */
    @Override
    public int updateBladePurchase(BladePurchase bladePurchase)
    {
        Long bladePurchaseId = bladePurchase.getBladePurchaseId();
        //数据库存储的数据
        BladePurchase PurchaseDB = selectBladePurchaseById(bladePurchaseId);
        //数据库存储的原数据的总量
        BladeAmount originAmounts = new BladeAmount();
        originAmounts.setOriginCompany(PurchaseDB.getOriginCompany());
        originAmounts.setBladeTypeId(PurchaseDB.getBladeTypeId());
        originAmounts.setBrand(PurchaseDB.getBrand());
        List<BladeAmount> amounts = bladeAmountMapper.selectBladeAmountList(originAmounts);
        BladeAmount originAmount = amounts.get(0);
        if (PurchaseDB.getOriginCompany().equals(bladePurchase.getOriginCompany())&&PurchaseDB.getBrand().equals(bladePurchase.getBrand())&&PurchaseDB.getBladeTypeId().equals(bladePurchase.getBladeTypeId()))
        {
            originAmount.setStorageAmount(originAmount.getStorageAmount()+bladePurchase.getPurchaseAmount()-PurchaseDB.getPurchaseAmount());
            bladeAmountMapper.updateBladeAmount(originAmount);
        }else {
            BladeAmount newAmounts = new BladeAmount();
            newAmounts.setBrand(bladePurchase.getBrand());
            newAmounts.setBladeTypeId(bladePurchase.getBladeTypeId());
            newAmounts.setOriginCompany(bladePurchase.getOriginCompany());
            List<BladeAmount> amounts1 = bladeAmountMapper.selectBladeAmountList(newAmounts);
            if (StringUtils.isEmpty(amounts1)){
                newAmounts.setStorageAmount(bladePurchase.getPurchaseAmount());
                bladeAmountMapper.insertBladeAmount(newAmounts);
            }else {
                BladeAmount newAmount = amounts1.get(0);
                newAmount.setStorageAmount(newAmount.getStorageAmount()+bladePurchase.getPurchaseAmount());
                bladeAmountMapper.updateBladeAmount(newAmount);
            }
            originAmount.setStorageAmount(originAmount.getStorageAmount()-PurchaseDB.getPurchaseAmount());
            bladeAmountMapper.updateBladeAmount(originAmount);
        }

        return bladePurchaseMapper.updateBladePurchase(bladePurchase);
    }

    /**
     * 批量删除刀片购买信息
     *
     * @param bladePurchaseIds 需要删除的刀片购买信息ID
     * @return 结果
     */
    @Override
    public int deleteBladePurchaseByIds(Long[] bladePurchaseIds)
    {
        for (Long bladePurchaseId : bladePurchaseIds) {
            BladePurchase bladePurchase = selectBladePurchaseById(bladePurchaseId);
            BladeAmount bladeAmount = new BladeAmount();
            bladeAmount.setBladeTypeId(bladePurchase.getBladeTypeId());
            bladeAmount.setBrand(bladePurchase.getBrand());
            bladeAmount.setOriginCompany(bladePurchase.getOriginCompany());
            List<BladeAmount> amounts = bladeAmountMapper.selectBladeAmountList(bladeAmount);
            BladeAmount bladeAmount1 = amounts.get(0);
            bladeAmount1.setStorageAmount(bladeAmount1.getStorageAmount() - bladePurchase.getPurchaseAmount());
            bladeAmountMapper.updateBladeAmount(bladeAmount1);
        }
        return bladePurchaseMapper.deleteBladePurchaseByIds(bladePurchaseIds);
    }

    /**
     * 删除刀片购买信息信息
     *
     * @param bladePurchaseId 刀片购买信息ID
     * @return 结果
     */
    @Override
    public int deleteBladePurchaseById(Long bladePurchaseId)
    {
        BladePurchase bladePurchase = selectBladePurchaseById(bladePurchaseId);
        BladeAmount bladeAmount = new BladeAmount();
        bladeAmount.setBladeTypeId(bladePurchase.getBladeTypeId());
        bladeAmount.setBrand(bladePurchase.getBrand());
        bladeAmount.setOriginCompany(bladePurchase.getOriginCompany());
        List<BladeAmount> amounts = bladeAmountMapper.selectBladeAmountList(bladeAmount);
        BladeAmount bladeAmount1 = amounts.get(0);
        bladeAmount1.setStorageAmount(bladeAmount1.getStorageAmount() - bladePurchase.getPurchaseAmount());
        bladeAmountMapper.updateBladeAmount(bladeAmount1);
        return bladePurchaseMapper.deleteBladePurchaseById(bladePurchaseId);
    }

    /**
     * 插入刀片购买表更新刀片量表
     * @param bladePurchase
     * @return
     */
    @Override
    public int insertPurchaseUpdateAmount(BladePurchase bladePurchase){
        BladeAmount bladeAmount = new BladeAmount();
        bladeAmount.setCreateTime(bladePurchase.getCreateTime());
        bladeAmount.setCreateBy(bladePurchase.getCreateBy());
        bladeAmount.setUpdateTime(bladePurchase.getUpdateTime());
        bladeAmount.setUpdateBy(bladePurchase.getUpdateBy());
        bladeAmount.setBladeTypeId(bladePurchase.getBladeTypeId());
        bladeAmount.setBrand(bladePurchase.getBrand());
        bladeAmount.setOriginCompany(bladePurchase.getOriginCompany());
        List<BladeAmount> bladeAmounts = bladeAmountMapper.selectBladeAmountList(bladeAmount);
        bladeAmount.setStorageAmount(bladePurchase.getPurchaseAmount());
        if (!StringUtils.isEmpty(bladeAmounts)) {
            BladeAmount amount = bladeAmounts.get(0);
            amount.setStorageAmount(amount.getStorageAmount() + bladePurchase.getPurchaseAmount());
            bladeAmountMapper.updateBladeAmount(amount);
        } else {
            bladeAmountMapper.insertBladeAmount(bladeAmount);
        }
        return insertBladePurchase(bladePurchase);

    }
}
